<template>
	<div class="common">
		<div class="common-top flex flex-align-center">
			<!-- <el-cascader
			 class="select-box"
			 placeholder="请选择分类"
                v-model="typeId"
                :options="options"
                :show-all-levels="false"
            ></el-cascader> -->
      <div class="change-stock-btn mode">
				<span>商品</span>
				<span :class="modeType=='PRODUCT'?'change-btn':'change-btn right-btn'" @click.stop="changeModeType">
					<i @click.stop=""></i>
				</span>
				<span>原料</span>
			</div>
			<template>
        <span class="goods-name-tit">商品名称:</span>
        <div class="search-box" style="z-index:999">
          <el-input class="input-box" placeholder="请输入名称" prefix-icon="el-icon-search" @blur="blurSearch" v-model="keyWords">
          </el-input>
          <ul class="ul-box" v-if="searchArr.length">
            <li v-for="item in searchArr" v-text="item.name" @click="getSearchItem(item)"></li>
          </ul>
        </div>
      </template>
			<div class="add-cap" v-if="!isBarStock">
				<button @click="firstAddCap">添加新规格</button>
			</div>
			<div class="change-stock-btn">
				<span>仓库</span>
				<span :class="isBarStock?'change-btn right-btn':'change-btn'" @click.stop="changeStockType">
					<i @click.stop=""></i>
				</span>
				<span>吧台</span>
			</div>
			<!-- <div class="search-btn">
				<span>检索</span>
			</div> -->
		</div>
		<div class="content-box">
      <template v-if="modeType=='PRODUCT'">
			<div class="content" v-if="!isBarStock">
				<div class="titleTable" @click.stop="doShowAddInp">
					<table>
						<tr>
							<th style="width:9%">序号</th>
							<th style="width:13%">商品名称</th>
							<th style="width:13%">规格(个/件)</th>
							<th style="width:13%">每件进价(元/件)</th>
							<th style="width:13%">库存(件)</th>
							<th style="width:13%">操作</th>
						</tr>
					</table>
				</div>
				<div class="scrollBox" @click.stop="doShowAddInp">
					<div class="scroll" :style="{'height':homeArr.length*10+'%'}" >
						<table class="conTable" @click.stop="doShowAddInp" v-if="homeArr.length>0">
							<tr v-for="(item,ind) in homeArr" class="capacity-tr">
								<td style="width:9%" v-text="ind+1+(currentPage-1)*pageSize"></td>
								<td style="width:13%" v-text="item.name" :title="item.name"></td>
								<!-- <td style="width:13%" v-text="item.typeName"></td> -->
								<td style="width:13%" v-text="item.capacity">
								</td>
								<td style="width:13%">
									<span v-if="item.isPrice" class="table-input">
										<input type="text" maxlength="8" v-model="item.purchasePrice" @keyup.enter="modify(item,ind,'price')" class="inputPrice">
										<span @click="modify(item,ind,'price')" class="sure-btn">确定</span>
									</span>
									<span v-else>
										<span v-text="item.purchasePrice"></span>
										<i class="iconfont icon-bianji" @click="homeArr[ind].isPrice=true"></i>
									</span>
								</td>
								<!-- <td style="width:13%" v-if="item.quantity>0">{{item.quantity}}</td> -->
								<td style="width:13%">{{item.quantity}}</td>
								<td style="width:13%" class="del" :style="homeArr.length>10?'padding-left:1.5%;':''" @click="delStandard(item)">删除</td>
								<!-- <td style="width:13%" v-else>-</td> -->
							</tr>
						</table>
						<div v-else class="first-in">
							<a href="javascript:;" @click="firstAddCap">暂无规格，请点击这里添加规格</a>
						</div>
					</div>
				</div>
				<div class="pager_add_btn clearfix" @click.stop="doShowAddInp">
					<div class="left _btn" @click="excel">
						导出数据
					</div>
					<span class="right">
						<el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="pageSize" layout="total, prev, pager, next,jumper" :total="total">
						</el-pagination>
					</span>
				</div>
			</div>
			<div class="content" v-if="isBarStock">
				<div class="titleTable">
					<table>
						<tr>
							<th style="width:9%">序号</th>
							<th style="width:13%">商品名称</th>
							<th style="width:13%">分类</th>
							<!-- <th style="width:13%">规格(个/件)</th> -->
							<!-- <th style="width:13%">每件进价(元/件)</th> -->
							<!-- <th style="width:13%">仓库库存(件)</th> -->
							<!-- <th style="width:13%">仓库库存(个)</th> -->
							<th style="width:13%">库存(个)</th>
						</tr>
					</table>
				</div>
				<div class="scrollBox">
					<div class="scroll" :style="{'height':homeArrForBar.length*10+'%'}">
						<table class="conTable">
							<tr v-for="(item,ind) in homeArrForBar">
								<td style="width:9%" v-text="ind+1+(currentPageForBar-1)*pageSizeForBar"></td>
								<td style="width:13%" v-text="item.name" :title="item.name"></td>
								<td style="width:13%" v-text="item.productType.name"></td>
								<td style="width:13%">
									{{item.quantity}}
								</td>
							</tr>
						</table>
					</div>
				</div>
				<div class="pager_add_btn clearfix">
					<div class="left _btn" @click="excel">
						导出数据
					</div>
					<span class="right">
						<el-pagination @current-change="handleCurrentChangeForBar" :current-page.sync="currentPageForBar" :page-size="pageSizeForBar" layout="total, prev, pager, next,jumper" :total="totalForBar">
						</el-pagination>
					</span>
				</div>
			</div>
      </template>
      <template v-if="modeType=='MATERIAL'">
        <div class="content" v-if="!isBarStock">
				<div class="titleTable" @click.stop="doShowAddInp">
					<table>
						<tr>
							<th style="width:9%">序号</th>
							<th style="width:13%">原料名称</th>
							<th style="width:13%">规格</th>
							<th style="width:13%">单位</th>
							<th style="width:13%">进价</th>
							<th style="width:13%">库存</th>
							<th style="width:13%">操作</th>
						</tr>
					</table>
				</div>
				<div class="scrollBox" @click.stop="doShowAddInp">
					<div class="scroll" :style="{'height':homeArr.length*10+'%'}" >
						<table class="conTable" @click.stop="doShowAddInp" v-if="homeArr.length>0">
							<tr v-for="(item,ind) in homeArr" class="capacity-tr">
								<td style="width:9%" v-text="ind+1+(currentPage-1)*pageSize"></td>
								<td style="width:13%" v-text="item.name" :title="item.name"></td>
								<!-- <td style="width:13%" v-text="item.typeName"></td> -->
								<td style="width:13%" v-text="item.capacity">
								</td>
                <td style="width:13%" v-text="item.unitName">
								</td>
								<td style="width:13%">
									<span v-if="item.isPrice" class="table-input">
										<input type="text" maxlength="8" v-model="item.purchasePrice" @keyup.enter="modify(item,ind,'price')" class="inputPrice">
										<span @click="modify(item,ind,'price')" class="sure-btn">确定</span>
									</span>
									<span v-else>
										<span v-text="item.purchasePrice"></span>
										<i class="iconfont icon-bianji" @click="homeArr[ind].isPrice=true"></i>
									</span>
								</td>
								<!-- <td style="width:13%" v-if="item.quantity>0">{{item.quantity}}</td> -->
								<td style="width:13%">{{item.quantity}}</td>
								<td style="width:13%" class="del" :style="homeArr.length>10?'padding-left:1.5%;':''" @click="delStandard(item)">删除</td>
								<!-- <td style="width:13%" v-else>-</td> -->
							</tr>
						</table>
						<div v-else class="first-in">
							<a href="javascript:;" @click="firstAddCap">暂无规格，请点击这里添加规格</a>
						</div>
					</div>
				</div>
				<div class="pager_add_btn clearfix" @click.stop="doShowAddInp">
					<div class="left _btn" @click="excel">
						导出数据
					</div>
					<span class="right">
						<el-pagination @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="pageSize" layout="total, prev, pager, next,jumper" :total="total">
						</el-pagination>
					</span>
				</div>
			</div>
			<div class="content" v-if="isBarStock">
				<div class="titleTable">
					<table>
						<tr>
							<th style="width:9%">序号</th>
							<th style="width:13%">原料名称</th>
							<!-- <th style="width:13%">规格(个/件)</th> -->
							<!-- <th style="width:13%">每件进价(元/件)</th> -->
							<!-- <th style="width:13%">仓库库存(件)</th> -->
							<!-- <th style="width:13%">仓库库存(个)</th> -->
							<th style="width:13%">库存</th>
						</tr>
					</table>
				</div>
				<div class="scrollBox">
					<div class="scroll" :style="{'height':homeArrForBar.length*10+'%'}">
						<table class="conTable">
							<tr v-for="(item,ind) in homeArrForBar">
								<td style="width:9%" v-text="ind+1+(currentPageForBar-1)*pageSizeForBar"></td>
								<td style="width:13%" v-text="item.name" :title="item.name"></td>
								<td style="width:13%">
									{{item.stockQty}}
								</td>
							</tr>
						</table>
					</div>
				</div>
				<div class="pager_add_btn clearfix">
					<div class="left _btn" @click="excel">
						导出数据
					</div>
					<span class="right">
						<el-pagination @current-change="handleCurrentChangeForBar" :current-page.sync="currentPageForBar" :page-size="pageSizeForBar" layout="total, prev, pager, next,jumper" :total="totalForBar">
						</el-pagination>
					</span>
				</div>
			</div>
      </template>
		</div>
		<div id="mask" v-if="doShowFirst" @click.stop="doShowFirst=false">
			<div class="first-in-add-cap" @click.stop="">
				<span class="goods-name-tit">名称:</span>
				<div class="search-box" style="z-index:999">
					<el-input class="input-box" clearable placeholder="请输入名称" prefix-icon="el-icon-search" @blur="blurSearch" v-model="keyWordsForFirstCap" @keyup.enter.native="saveFirstCap">
					</el-input>
					<ul class="ul-box" v-if="searchArrForFirst.length">
						<li v-for="item in searchArrForFirst" v-text="item.name" @click="getSearchItemForFirst(item)"></li>
					</ul>
				</div>
				<span class="first-cap">输入规格:</span>
				<div class="search-box">
					<el-input class="input-box" placeholder="请输入规格，如：10" prefix-icon="" v-model="firstCap" @keyup.enter.native="saveFirstCap">
					</el-input>
				</div>
				<div class="saveFirst">
					<button @click="saveFirstCap">保存</button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
export default {
  props: ["storeId"],
  data() {
    return {
      modeType:"PRODUCT",
      coverImgMaxSize: 5 * 1024 * 1024, // 图片最大字节数
      oldImgBase64: "", // 原图base64
      showCrop: false, // 控制图片裁剪显示隐藏
      curCropW: 600, // 裁剪宽度
      curCropH: 600, // 裁剪高度
      billData: {
        typeValue: "",
        notes: "",
        totalAmount: 0, //用户手动输入的总金额
        imgUrl: ""
      },
      typeOptions: [
        { value: "NORMAL", label: "正常入库" },
        { value: "CHECK_IN", label: "盘点入库" },
        { value: "REFUND", label: "退单入库" },
        { value: "STORAGE", label: "仓储（仓库至吧台）" },
        { value: "ALLOCATION", label: "店铺调拨" },
        { value: "DECLINE", label: "吧台下架" },
        { value: "PURCHASE", label: "采购" },
        { value: "SALES", label: "销售出库" },
        { value: "CHECK", label: "盘点出库" },
        { value: "STOCK_OUT_ONE", label: "吧台到仓库" }
      ],
      typeValue: "",
      keyWords: "",
      keyWordsForFirstCap: "",
      currentPage: 1,
      currentPageForBar: 1,
      pageSize: 10, //仓库分页数据
      pageSizeForBar: 20, //吧台的分页数据
      total: 0, //仓库商品总数
      totalForBar: 0, //吧台商品总数
      dialogBtn: false,
      options: [],
      typeId: [""],
      searchArr: [], //搜索的商品列表
      searchArrForBar: [], //搜索的吧台商品列表
      homeArr: [], //首页仓库商品列表
      homeArrForBar: [], //首页吧台商品列表
      curProduct: {}, //当前要入库出库的商品,
      isBarStock: false, //首页当前是否是吧台库存,
      isSearch: false, //判断当前是否是搜索过后的结果列表,
      searchItem: {}, //当前搜索的对象
      showAddInp: false, //是否显示输入框
      doShowStandard: false, //是否显示规格
      nowInd: 0, //当前选择的规格索引
      standard: "", //添加的规格名字
      capacityArr: [], //规格列表,
      searchArrForFirst: [], //初次添加规格
      doShowFirst: false,
      CapPurMoney: "", //添加规格时所需要的单价,
      searchItemForFirst: null, //第一次添加规格时所选的搜索商品项
      firstCap: ""
    };
  },
  watch: {
    keyWords(val) {
      this.searchArr = [];
      if (!val) {
        if (this.isBarStock) {
          this.getBarData(this.currentPageForBar - 1);
        } else {
          this.getHomeData(this.currentPage - 1);
          //隐藏规格相关
          this.doShowAddInp();
        }
        return;
      }
      // 搜索
      this.$http({
        url: "search",
        method: "GET",
        params: {
          storeId: this.storeId,
          q: val,
          mold:this.modeType,
        }
      }).then(res => {
        this.searchArr = res.data;
      });
    },
    keyWordsForFirstCap(val) {
      if (this.searchItemForFirst) {
        if (val === this.searchItemForFirst.name) {
          return;
        }
      }
      this.searchArrForFirst = [];
      if (!val) {
        if (this.isBarStock) {
          this.getBarData(this.currentPageForBar - 1);
        } else {
          this.getHomeData(this.currentPage - 1);
          this.keyWords = "";
          //隐藏规格相关
          this.doShowAddInp();
        }
        return;
      }
      // 搜索
      if(this.modeType == "MATERIAL"){
        this.$http({
          url: "query/list",
          method: "GET",
          params: {
            storeId: this.storeId,
            name: val
          }
        }).then(res => {
          this.searchArrForFirst = res.data;
        });
      }else{
        this.$http({
          url: "search",
          method: "GET",
          params: {
            storeId: this.storeId,
            q: val
          }
        }).then(res => {
          this.searchArrForFirst = res.data;
        });
      }
      
    },
    typeId(val) {
      this.getHomeData(null, val[val.length - 1]);
    },
    doShowFirst(val) {
      this.searchItemForFirst = null;
      this.CapPurMoney = "";
      this.firstCap = "";
      this.keyWordsForFirstCap = "";
    }
  },
  methods: {
    changeModeType(){
      this.modeType == "PRODUCT"?this.modeType="MATERIAL":this.modeType="PRODUCT";
      if (this.isBarStock) {
        this.homeArrForBar = [];
        this.getBarData(0);
      } else {
        this.homeArr = [];
        this.getHomeData(0);
        //隐藏规格相关
        this.doShowAddInp();
      }
    },
    saveFirstCap() {
      if (!this.searchItemForFirst) {
        this.$message({
          showClose: true,
          message: "请选择商品",
          type: "error",
          duration: 2000
        });
        return;
      }
      // if (!/^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$/.test(this.CapPurMoney)) {
      //   this.$message({
      //     showClose: true,
      //     message: "价格输入有误",
      //     type: "error",
      //     duration: 2000
      //   });
      //   return;
      // }
      if (parseInt(this.firstCap) > 999) {
        this.$message({
          message: "规格输入过大",
          type: "error",
          duration: 2000
        });
        return;
      }
      // if (parseInt(this.firstCap) < 2) {
      //   this.$message({
      //     showClose: true,
      //     message: "商品规格必须大于1，否则请直接到菜单“吧台”中，进行“上架操作”。",
      //     type: "error",
      //     duration: 3000
      //   });
      //   return;
      // }
      if (!/^[1-9][0-9]*$/.test(this.firstCap)) {
        // if(this.standard === '0'){
        // 	doAddStandard();
        // 	return;
        // }
        this.$message({
          showClose: true,
          message: "输入规格有误",
          type: "error",
          duration: 2000
        });
        return;
      }
      this.$http({
        url: "add-indexCa",
        method: "POST",
        data: {
          productId: this.searchItemForFirst.id,
          productName: this.searchItemForFirst.name,
          capacity: this.firstCap,
          storeId: this.storeId,
          mold: this.modeType,
          purchasePrice: this.CapPurMoney,
          unitId:this.modeType=="MATERIAL"?this.searchItemForFirst.unitId:undefined
        }
      }).then(res => {
        console.log(res);
        let data = res.data;
        this.homeArr = [];
        this.homeArr.push(data.data);
        this.doShowFirst = false;
        this.$message({
          showClose: true,
          message: data.msg,
          type: "success",
          duration: 2000
        });
      });
    },
    getSearchItemForFirst(item) {
      this.searchItemForFirst = item;
      this.keyWordsForFirstCap = item.name;
      this.searchArrForFirst = [];
      console.log(item);
    },
    //初始界面添加规格
    firstAddCap() {
      this.doShowFirst = true;
    },
    // 规格相关
    // 改变当前商品规格
    showNowStandard(item) {
      this.$http({
        url: "get-invCa",
        method: "GET",
        params: {
          invId: item.id,
          cap: item.capacity
        }
      }).then(res => {
        this.homeArr.map((ele, index) => {
          if (ele.productId === res.data.productId) {
            res.data.isPrice = false;
            res.data.whole = 0;
            this.homeArr.splice(index, 1, res.data);
          }
        });
      });
    },
    // 是否显示规格输入框
    doShowAddInp() {
      this.showAddInp = false;
      this.doShowStandard = false;
    },
    // 是否显示规格
    showStandard(index, item) {
      console.log(item);
      // return;
      this.nowInd = index;
      this.doShowStandard = !this.doShowStandard;
      this.showAddInp = false;
      // this.doShowStandard?this.showAddInp = false:this.showAddInp=true;
      // 调取规格列表
      if (this.doShowStandard) {
        this.getStandardList(item);
      }
    },
    // 获取规格列表
    getStandardList(item) {
      this.$http({
        url: "/find-capacity",
        method: "GET",
        params: {
          productId: item.productId,
          storeId: this.storeId
        }
      }).then(res => {
        this.capacityArr = res.data;
      });
    },
    // 添加规格
    addStandard(item) {
      console.log(this.standard);
      console.log(item);
      console.log(this.searchItemForFirst)
      if (parseInt(this.standard) > 999) {
        this.$message({
          message: "规格输入过大",
          type: "error",
          duration: 2000
        });
        return;
      }
      let doAddStandard = () => {
        this.$http({
          url: "/add-capacity",
          method: "POST",
          data: {
            id: item.id,
            storeId: item.storeId,
            productId: item.productId,
            unitId: this.searchItemForFirst.unitId,
            name: item.name,
            mold: this.modeType,
            capacity: this.standard
          }
        }).then(res => {
          console.log(res);
          if (res.status === 200) {
            this.$message({
              message: "添加成功",
              type: "success",
              duration: 2000
            });
            this.showAddInp = false;
            this.standard = "";
            this.getStandardList(item);
            // 添成功后获取数据,如果当前处于搜索就调取搜索的结果
            if (this.isSearch && !this.isBarStock) {
              this.getSearchItem(this.searchItem, this.currentPage - 1);
            } else {
              this.getHomeData(this.currentPage - 1);
            }
          }
        });
      };
      if (!this.standard) {
        this.$message({
          showClose: true,
          message: "请输入规格",
          type: "error",
          duration: 2000
        });
      } else {
        if (!/^[1-9][0-9]*$/.test(this.standard)) {
          // if(this.standard === '0'){
          // 	doAddStandard();
          // 	return;
          // }
          this.$message({
            showClose: true,
            message: "输入规格有误",
            type: "error",
            duration: 2000
          });
        } else {
          doAddStandard();
        }
      }
    },
    // 删除规格
    delStandard(item) {
      this.$confirm("确认删除此规格？", "删除规格", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: "delete-invId",
          method: "GET",
          params: {
            invId: item.id
          }
        }).then(res => {
          if (res.status === 200) {
            if (res.data.code === 400) {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: "error",
                duration: 2000
              });
              return;
            }
            if (res.data.code === 200) {
              this.$message({
                showClose: true,
                message: "删除成功",
                type: "success",
                duration: 2000
              });
            //   this.getStandardList(itemForProduct);
              // 删除成功后获取列表
              if (this.isSearch && !this.isBarStock) {
                this.getSearchItem(this.searchItem, this.currentPage - 1);
              } else {
                this.getHomeData(this.currentPage - 1);
              }
            }
          }
        });
      });
      // console.log(itemForProduct);
    },
    // 规格相关结束
    changeStockType() {
      this.isBarStock = !this.isBarStock;
      // 如果是吧台库存的话，获取吧台商品数据。首页一进入是先获取仓库数据
      if (this.isBarStock) {
        this.getBarData(0);
      }else{
        this.getHomeData(0);
      }
    },
    //翻页For仓库
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      let typeId = this.typeId ? this.typeId[this.typeId.length - 1] : null;
      //以下代码用作搜索之后的翻页
      if (this.isSearch && !this.isBarStock) {
        this.getSearchItem(this.searchItem, val - 1);
      } else {
        this.getHomeData(val - 1);
      }
    },
    // 翻页For吧台
    handleCurrentChangeForBar(val) {
      console.log(`当前页: ${val}`);
      let typeId = this.typeId ? this.typeId[this.typeId.length - 1] : null;
      //以下代码用作搜索之后的翻页
      if (this.isSearch && this.isBarStock) {
        this.getSearchItem(this.searchItem, val - 1);
      } else {
        this.getBarData(val - 1);
      }
    },
    getBarData(page = 0) {
      this.isSearch = false;
      this.$http({
        url: "stock",
        method: "GET",
        params: {
          storeId: this.storeId,
          mold:this.modeType,
          page
        }
      }).then(res => {
        this.homeArrForBar = res.data.content;
        this.totalForBar = res.data.totalElements;
        console.log(res);
      });
    },
    //根据id获取某个商品
    getSearchItem(item, page = 0) {
      this.searchItem = item;
      this.isSearch = true;
      //隐藏规格相关的东西
      this.doShowAddInp();
      if (!this.isBarStock) {
        // 仓库的搜索
        this.$http({
          url:"get/product",
          method: "GET",
          params: {
            id: item.id,
            mold:this.modeType,
            bat: "",
            page,
            index: "index"
          }
        }).then(res => {
          let data = res.data.content;
          this.homeArr = [];
          for (var i = 0; i < data.length; i++) {
            let obj = {
              isPrice: false, //价格是否处于修改状态
              id: data[i].id,
              productId: data[i].productId,
              name: data[i].name,
              storeId: data[i].storeId,
              capacity: data[i].capacity, //规格
              purchasePrice: data[i].purchasePrice, //每件进价
              unitName: data[i].unitName, //每件进价
              quantity: data[i].quantity //仓库库存
            };
            this.homeArr.push(obj);
          }
          this.total = res.data.totalElements;
        });
      } else {
        this.$http({
          url:"get/product",
          method: "GET",
          params: {
            id: item.id,
            mold:this.modeType,
            bat: "bat",
            page,
            index: "index"
          }
        }).then(res => {
          this.homeArrForBar = res.data.content;
          this.totalForBar = res.data.totalElements;
        });
      }
    },
    blurSearch() {
      setTimeout(() => {
        this.searchArr = [];
      }, 800);
    },
    //警告修改规格
    willModify(ind) {
      this.homeArr[ind].isStock = true;
    },
    //修改规格 和 价格
    modify(item, ind, boo) {
      if (boo == "stock") {
        //修改规格
        if (parseInt(item.stockQuantityRatio) <= 0) {
          this.$message({
            showClose: true,
            message: item.name + "格式有误",
            type: "error"
          });
          return true;
        }
        this.$alert(
          "商品规格修改为：" + item.stockQuantityRatio + "，建议新建商品！",
          "确认修改",
          {
            confirmButtonText: "提交"
            // cancelButtonText: '取消',
            // type: 'error'
          }
        )
          .then(() => {
            this.$http
              .post("/modify-format", {
                productId: item.id,
                stockQuantityRatio: parseInt(item.stockQuantityRatio)
              })
              .then(res => {
                this.homeArr[ind].isStock = false;
                this.$message({
                  showClose: true,
                  message: "修改成功",
                  type: "success",
                  duration: 2000
                });
              });
          })
          .catch(() => {
            this.homeArr[ind].isStock = false;
          });
      } else if (boo == "price") {
        //修改价格
        if (!/^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$/.test(item.purchasePrice)) {
          this.$message({
            showClose: true,
            message: item.name + "价格有误",
            type: "error",
            duration: 2000
          });
          return true;
        }
        this.$http
          .post("/update", {
            id: item.id,
            price: parseFloat(item.purchasePrice)
          })
          .then(res => {
            console.log(res);
            this.homeArr[ind].isPrice = false;
            this.$message({
              showClose: true,
              message: "修改成功",
              type: "success",
              duration: 2000
            });
          });
      }
    },
    getHomeData(page = 0) {
      this.isSearch = false;
      this.$http({
        url: "inventories",
        method: "GET",
        params: {
          storeId: this.storeId,
          page: page,
          mold:this.modeType,
          name: ""
        }
      }).then(res => {
        let data = res.data.content;
        this.homeArr = [];
        for (var i = 0; i < data.length; i++) {
          let obj = {
            isPrice: false, //价格是否处于修改状态
            id: data[i].id,
            productId: data[i].productId,
            name: data[i].name,
            storeId: data[i].storeId,
            capacity: data[i].capacity, //规格
            purchasePrice: data[i].purchasePrice, //每件进价
            unitName: data[i].unitName, //每件进价
            quantity: data[i].quantity //仓库库存
          };
          this.homeArr.push(obj);
        }
        this.total = res.data.totalElements;
        // console.log(this.total);
      });
    },
    //获取分类
    getHomeType() {
      this.$http.get("/product-types?storeId=" + this.storeId).then(res => {
        this.options.push({ label: "全部", value: "" });
        for (var i = 0; i < res.data.length; i++) {
          let obj = {
            value: res.data[i].id,
            label: res.data[i].name
          };
          if (res.data[i].productTypeVos.length) {
            let son = res.data[i].productTypeVos;
            let arr = [{ value: res.data[i].id, label: res.data[i].name }];
            for (var j = 0; j < son.length; j++) {
              let sonobj = {
                value: son[j].id,
                label: "- " + son[j].name
              };
              arr.push(sonobj);
            }
            obj.children = arr;
          }
          this.options.push(obj);
        }
        // console.log(this.options)
      });
    },
    //数据导出
    excel() {
      if (!this.isBarStock) {
        this.$http({
          url: "inventories",
          method: "GET",
          params: {
            storeId: this.storeId,
            mold:this.modeType,
            page: "",
						size:10000,
            name: ""
          }
        }).then(res => {
          let data = res.data.content;
          if(this.modeType == "PRODUCT"){
            let html =
              '<tr  style="font-size:18px;font-weight:900;text-align:center">' +
              "<td>商品名称</td>" +
              "<td>规格(个/件)</td>" +
              "<td>每件进价(元/件)</td>" +
              "<td>仓库库存(件)</td>" +
              "</tr>";
            for (var i = 0; i < data.length; i++) {
              html +=
                '<tr  style="font-size:14px;text-align:center">' +
                "<td>" +
                data[i].name +
                "</td>" +
                "<td>" +
                data[i].capacity +
                "</td>" +
                "<td>" +
                data[i].purchasePrice +
                "</td>" +
                "<td>" +
                data[i].quantity +
                "</td>" +
                "</tr>";
            }
            let table = document.createElement("table");
            document.body.appendChild(table);
            table.style.display = "none";
            // table.style.borderCollapse='collapse';
            table.innerHTML = html;
            window.method5(table);
          }else{
            let html =
              '<tr  style="font-size:18px;font-weight:900;text-align:center">' +
              "<td>原料名称</td>" +
              "<td>规格</td>" +
              "<td>进价(</td>" +
              "<td>仓库库存</td>" +
              "</tr>";
            for (var i = 0; i < data.length; i++) {
              html +=
                '<tr  style="font-size:14px;text-align:center">' +
                "<td>" +
                data[i].name +
                "</td>" +
                "<td>" +
                data[i].capacity +
                "</td>" +
                "<td>" +
                data[i].purchasePrice +
                "</td>" +
                "<td>" +
                data[i].quantity +
                "</td>" +
                "</tr>";
            }
            let table = document.createElement("table");
            document.body.appendChild(table);
            table.style.display = "none";
            // table.style.borderCollapse='collapse';
            table.innerHTML = html;
            window.method5(table);
          }
          
        });
      } else {
        this.$http({
          url: "stock",
          method: "GET",
          params: {
            storeId: this.storeId,
            mold:this.modeType,
						size:10000,
            page: ""
          }
        }).then(res => {
          let data = res.data.content;
          if(this.modeType == "PRODUCT"){
            let html =
              '<tr  style="font-size:18px;font-weight:900;text-align:center">' +
              "<td>商品名称</td>" +
              "<td>分类</td>" +
              "<td>吧台库存</td>" +
              "</tr>";
            for (var i = 0; i < data.length; i++) {
              html +=
                '<tr  style="font-size:14px;text-align:center">' +
                "<td>" +
                data[i].name +
                "</td>" +
                "<td>" +
                data[i].productType.name +
                "</td>" +
                "<td>" +
                data[i].quantity +
                "</td>" +
                "</tr>";
            }
            let table = document.createElement("table");
            document.body.appendChild(table);
            table.style.display = "none";
            // table.style.borderCollapse='collapse';
            table.innerHTML = html;
            window.method5(table);
          }else{
            let html =
              '<tr  style="font-size:18px;font-weight:900;text-align:center">' +
              "<td>名称</td>" +
              "<td>吧台库存</td>" +
              "</tr>";
            for (var i = 0; i < data.length; i++) {
              html +=
                '<tr  style="font-size:14px;text-align:center">' +
                "<td>" +
                data[i].name +
                "</td>" +
                "<td>" +
                data[i].stockQty +
                "</td>" +
                "</tr>";
            }
            let table = document.createElement("table");
            document.body.appendChild(table);
            table.style.display = "none";
            // table.style.borderCollapse='collapse';
            table.innerHTML = html;
            window.method5(table);
          }

          
        });
      }
    }
  },
  mounted() {
    this.getHomeData();
  }
};
</script>
<style lang="scss" scoped>
@import "../../../static/css/common.scss";
@import "home";
@import "./home.scss";
.mode{
  // color: #33c725;
  cursor: pointer;
  user-select: none;
  padding-left: 30px;
}
</style>